﻿using MeterialManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MeterialManager
{
    public partial class FrmSetCategory : Form
    {
        private DelBindDgv _delBindDgv;
        private Category _category;
        public FrmSetCategory(DelBindDgv delBindDgv)
        {
            InitializeComponent();
            _delBindDgv = delBindDgv;
        }

        public FrmSetCategory(DelBindDgv delBindDgv,int categoryId):this(delBindDgv)
        {
            _category = Category.GetListCategory().Find(m => m.CategoryId == categoryId);
        }

        private void FrmSetCategory_Load(object sender, EventArgs e)
        {
            if(_category != null) {
                tbCategoryCode.Text = _category.CategoryCode;
                tbCategoryName.Text = _category.CategoryName;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string categoryCode = tbCategoryCode.Text.Trim();
            string categoryName = tbCategoryName.Text.Trim();
            if(_category == null)
            {
                Category category = new Category()
                {
                    CategoryCode = categoryCode,
                    CategoryName = categoryName
                };
                Category.Insert(category);
                MessageBox.Show("添加成功！");
            }
            else
            {
                _category.CategoryCode = categoryCode;
                _category.CategoryName = categoryName;
                Category.Update(_category);
                MessageBox.Show("修改成功！");
            }
            _delBindDgv();
            this.Close();
        }
    }
}
